In people’s daily life, we can open a PDF Document by right clicking the open option as well as using C#, VB.NET or other programming languages. Both methods are available as long as you have a PDF Document, but for PDF itself, it has no viewing function, thus, we need to use PDF Viewer to help us view it. This article is designed to open a PDF Document with C#, VB.NET via PDF Viewer by two methods.
In order to realize this function quickly, I use a PDF Viewer component for .NET Spire. PDFViewer to help me. Spire.PDFViewer is a totally independent .NET library which is designed for viewing PDF files from .NET application. It does NOT require Adobe Read or any other 3rd party software/library installed on system. Please look at the following procedure.
In order to realize this function quickly, I use a PDF Viewer component for .NET Spire. PDFViewer to help me. Spire.PDFViewer is a totally independent .NET library which is designed for viewing PDF files from .NET application. It does NOT require Adobe Read or any other 3rd party software/library installed on system. Please look at the following procedure.
Procedure:
Step 1. Create a new project.
1. Freely Download Spire.PDFViewer.
2. Create a new project in Visual Studio and add a toolScript in Form1.
3. Set its target Framework to be .NET Framwork 2 or above.
4. Add Spire.PdfViewer. Forms as reference in Project. And add using at the top of the method. Please see below:
C#
using System.IO;
using Spire.PdfViewer.Forms;
VB.NET
Imports Sytem. IO
Imports Spire.PdfViewer.Forms
Step 2. Open a PDF Document with C#, VB.NET via Spire.PDFViewer.
Method one to open PDF document with C#, VB.NET via Spire.PDFViewer.
This method is to directly load a PDF file from system.
C# Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
string pdfDoc = @"D:\michelle\e-iceblue\Spire.Office.pdf";
if (File.Exists(pdfDoc))
{
this.pdfDocumentViewer1.LoadFromFile(pdfDoc);
}
}
}
VB.NET Code:
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs)
End Sub
Private Sub toolStripButton1_Click(sender As Object, e As EventArgs)
Dim pdfDoc As String = "D:\michelle\e-iceblue\Spire.Office.pdf"
If File.Exists(pdfDoc) Then
Me.pdfDocumentViewer1.LoadFromFile(pdfDoc)
End If
End Sub
End Class
Method Two to open PDF document with C#, VB.NET via Spire.PDFViewer.
This method is for users to choose the PDF File from the dialog box.
C# Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "PDF document (*.pdf)|*.pdf";
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
try
{
string pdfFile = dialog.FileName;
this.pdfDocumentViewer1.LoadFromFile(pdfFile);
}
catch (Exception exe)
{
MessageBox.Show(exe.Message, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
VB.NET Code:
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs)
End Sub
Private Sub toolStripButton1_Click(sender As Object, e As EventArgs)
Dim dialog As New OpenFileDialog()
dialog.Filter = "PDF document (*.pdf)|*.pdf"
Dim result As DialogResult = dialog.ShowDialog()
If result = DialogResult.OK Then
Try
Dim pdfFile As String = dialog.FileName
Me.pdfDocumentViewer1.LoadFromFile(pdfFile)
Catch exe As Exception
MessageBox.Show(exe.Message, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try
End If
End Sub
End Class
Step 3. Launch the file.
Press F5, you can see Form1 display itself as picture 3.1.
Then click “open” in the Form.
When you use method one, you can see the PDF document content shows in the Form1.
When you use method two, you can choose the PDF Document by yourself in a dialogbox. And then preview it.
Note: I change the "textScript" to be "open".
Preview
More About Spire.PDFViewer
As a standalone component, Spire.PDFViewer supports viewing formatted PDF such as embedded with hyperlinks. PDF with basic fonts (TrueType, Type 0, Type 1, Type 3, OpenType and CJK font) are also supported. Besides, this PDF Viewer supports PDF with filled text, clipped text, stroked text and supports PDF with lines, curve and table. Click to know more…